package com.xuqm.sdhbwfu.app.ui

import android.content.Intent
import android.os.Bundle
import com.xuqm.base.common.ToolsHelper
import com.xuqm.base.ui.BaseActivity
import com.xuqm.sdhbwfu.app.R
import com.xuqm.sdhbwfu.app.databinding.ActivityPmMaterialBinding

class PmMaterialActivity : BaseActivity<ActivityPmMaterialBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_pm_material

    override fun fullscreen(): Boolean = true

    private var position = -1


    override fun initData() {
        super.initData()
        position = intent.getIntExtra("position", -1)
        val quantity = intent.getIntExtra("quantity", -1)
        val name = intent.getStringExtra("name")
        val specification = intent.getStringExtra("specification")

        if (-1 != quantity) {
            binding.quantity.setText("$quantity")
        }
        name?.also {
            binding.name.setText(it)
        }
        specification?.also {
            binding.specification.setText(it)
        }

    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        binding.confirm.setOnClickListener {
            val quantity = binding.quantity.text.toString()
            val name = binding.name.text.toString()
            val specification = binding.specification.text.toString()
            if (specification.isEmpty()
                || quantity.isEmpty()
                || name.isEmpty()
            ) {
                ToolsHelper.showMessage("填写项不能为空")
                return@setOnClickListener
            }

            setResult(RESULT_OK, Intent().apply {
                putExtra("position", position)
                putExtra("name", name)
                putExtra("specification", specification)
                putExtra("quantity", quantity.toInt())
            })
            finish()
        }
        binding.cancel.setOnClickListener {
            setResult(RESULT_CANCELED)
            finish()
        }
    }

}